package com.runa.monitor.platinterface.extend.third.newester.validator;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @Author: lyd
 * @Description:
 * @Date: 2021/11/3 10:18
 * @Version: 1.0
 */
public class DateValidator implements ConstraintValidator<Date, String> {
    private String datePattern;

    @Override
    public void initialize(Date constraintAnnotation) {
        this.datePattern = constraintAnnotation.datePattern();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)) {
            return true;
        }
        boolean flag = false;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
            simpleDateFormat.parse(value);
            flag = true;
        } catch (ParseException exception) {
            flag = false;
        }
        return flag;
    }
}